<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="searchInpt"
        clearable
        @keyup.enter.native="getTaskRecord()"
        :placeholder="searchPlaceholder()"
        style="width:350px;"
      >
        <el-select
          v-model="searchSelectValue"
          slot="prepend"
          placeholder="请选择"
          style="width:100px;"
        >
          <el-option
            v-for="item in searchSelectOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          @click="getTaskRecord()"
          icon="el-icon-search"
          slot="append"
          :loading="searchLoading"
        >搜索</el-button>
      </el-input>

      <div class="lz-tips">
        <label>活动筛选:</label>
      </div>
      <el-select
        v-model="taskStatusValue"
        placeholder="活动筛选"
        @change="getTaskRecord()"
      >
        <el-option
          v-for="item in taskStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <div class="lz-tips">
        <label>状态筛选:</label>
      </div>
      <el-select
        v-model="taskCompleteValue"
        placeholder="状态筛选"
        @change="getTaskRecord()"
      >
        <el-option
          v-for="item in taskCompleteStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div>
      <Table
        :table="dataTable"
        @view="view"
        @check="check"
      >
        <template
          slot-scope="props"
          slot="imgUrl"
        >
          <el-button
            type="text"
            @click="showImgDialogBox(props.obj.row.imgUrl)"
            size="small"
          >查看图片</el-button>

        </template>
      </Table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="存档内容"
      :visible.sync="showImgDialog"
      top="4%"
      width="40%"
    >
      <div class="demo-image__lazy">
        <div
          v-for="(pic,index) in picture"
          :key="index"
        >
          <vue-photo-zoom-pro :url="pic"></vue-photo-zoom-pro>
        </div>
      </div>
    </el-dialog>

    <el-dialog
      title="上传&审核图片"
      :visible.sync="showUploadImgDialog"
      width="30%"
    >
      <div>
        <p>
          活动:{{uploadImgData.taskName}}
        </p>
        <p>
          绑定手机:{{uploadImgData.userName}}
        </p>
        <p>
          蜜豆:{{uploadImgData.reward}}
        </p>
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-remove="handleRemove"
          name="files"
          :on-success="handleAvatarSuccess"
          :headers="imgHeaders"
          multiple
          :file-list="fileList"
          list-type="picture"
        >
          <el-button
            size="small"
            type="primary"
          >选择图片</el-button>
        </el-upload>
        <div style="margin-top:10px;">
          <el-input
            v-model="uploadImgData.orderNum"
            placeholder="请输入订单号"
          ></el-input>
        </div>
        <div style="margin-top:10px;">
          <el-button
            size="small"
            type="primary"
            @click="getAuditTask()"
          >提交</el-button>
        </div>
      </div>
    </el-dialog>

    <el-dialog
      title="审核图片"
      :visible.sync="viewImgDialog"
      width="40%"
      top="20px"
    >
      <div class="demo-image__lazy">
        <div
          v-for="(pic,index) in picture"
          :key="index"
        >
          <vue-photo-zoom-pro :url="pic"></vue-photo-zoom-pro>
        </div>
      </div>
      <div>
        <h4>审核结果：</h4>
        <el-radio
          v-model="viewImgRadio"
          label="10"
        >审核通过</el-radio>
        <el-radio
          v-model="viewImgRadio"
          label="5"
        >审核不通过</el-radio>
      </div>
      <div v-if="viewImgRadio == '5'">
        <h4>请选择失败原因：</h4>
        <el-radio
          v-model="viewImgErrorRadio"
          label="1"
        >图片不清晰</el-radio>
        <el-radio
          v-model="viewImgErrorRadio"
          label="2"
        >图片不符合要求</el-radio>
      </div>
      <div style="margin-top:10px; text-align:center;">
        <el-button
          size="small"
          type="primary"
          @click="getAuditUpload()"
        >提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchTaskRecord,
  fetchAuditTask,
  fetchTaskList,
  fetchAuditUpload
} from "@/api/table";
import format from "date-format";
import { formatDateTime } from "@/utils/common";
import Table from "@/components/Table/index";
import { getToken } from "@/utils/auth";

export default {
  data() {
    return {
      dataTable: {
        loading: false,
        border: true,
        hasOperation: true,
        tr: [],
        data: [],
        operation: {}
      },
      //搜索
      searchInpt: "",
      searchLoading: false,
      searchSelectValue: "userId",
      searchSelectOption: [
        {
          value: "userId",
          label: "用户ID"
        },
        {
          value: "userName",
          label: "手机号"
        },
        {
          value: "nickName",
          label: "用户昵称"
        }
      ],
      //总页码
      count: 0,
      //单页行数
      limit: 20,
      //页码
      currentPage: 1,
      //活动删选
      taskStatus: [
        {
          value: "",
          label: "全部活动"
        }
      ],
      taskStatusValue: "",
      //任务完成状态删选
      taskCompleteStatus: [
        {
          value: "1",
          label: "待领取"
        },
        {
          value: "2",
          label: "进行中"
        },
        {
          value: "3",
          label: "已领取"
        },
        {
          value: "5",
          label: "待审核"
        }
        // {
        //   value: "6",
        //   label: "已完成"
        // }
      ],
      taskCompleteValue: "2",
      tableloading: false,
      tableData: [],
      showImgDialog: false,
      picture: [],
      showUploadImgDialog: false,
      fileList: [],
      //上传图片链接
      uploadUrl: "",
      imgHeaders: {
        Authorization: ""
      },
      uploadImgData: {
        orderNum: "",
        imgList: [],
        taskName: "",
        userName: "",
        taskRecordId: "",
        reward: ""
      },
      viewImgDialog: false,
      //审核图片
      viewImgRadio: "5",
      viewImgErrorRadio: "1",
      viewImgData: {
        taskRecordId: ""
      }
    };
  },
  components: {
    Table
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.imgHeaders.Authorization = getToken();
      this.getTaskRecord();
      this.getTaskLis();
      //上传图片链接
      this.uploadUrl = process.env.BASE_API + "/picture/upload";
    },
    check(index, row) {
      row.taskName == "好友福利一起享"
        ? this.uploadImgDialogBox(row)
        : this.$message({
            type: "error",
            message: "无需审核"
          });
    },
    view(index, row) {
      this.viewImgRadio = "10";
      this.viewImgErrorRadio = "1";
      this.viewImgDialog = true;
      this.viewImgData.taskRecordId = row.taskRecordId;
      this.picture = row.imgUrl.split(",");
    },
    searchPlaceholder() {
      if (this.searchSelectValue == "userId") {
        return "请输入用户ID";
      } else if (this.searchSelectValue == "userName") {
        return "请输入手机号";
      } else if (this.searchSelectValue == "nickName") {
        return "请输入用户昵称";
      }
    },
    showImgDialogBox(img) {
      if (img !== undefined) {
        this.showImgDialog = true;
        this.picture = img.split(",");
      } else {
        this.$message({
          type: "error",
          message: "无图片"
        });
      }
    },
    handleRemove(file, fileList) {
      this.uploadImgData.imgList = fileList;
    },
    handleAvatarSuccess(res, file, fileList) {
      if (res.status === 200) {
        this.uploadImgData.imgList = fileList;
        this.$message.success("上传成功！");
      } else {
        this.$message.error(res.msg);
      }
    },
    uploadImgDialogBox(info) {
      this.fileList = [];
      this.uploadImgData.orderNum = "";
      this.showUploadImgDialog = true;
      this.uploadImgData.taskName = info.taskName;
      this.uploadImgData.userName = info.userName;
      this.uploadImgData.taskRecordId = info.taskRecordId;
      this.uploadImgData.reward = info.reward;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTaskRecord();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getTaskRecord();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    //时间格式化
    formatterTime(t) {
      if (t) {
        const newTime = format("yyyy-MM-dd hh:mm:ss", new Date(t)).split(" ");
        let time = "";
        time += newTime[0] + "\n" + newTime[1];
        return time;
      } else {
        return "";
      }
    },
    async getAuditUpload() {
      let radioTxt;
      if (this.viewImgRadio == "5" && this.viewImgErrorRadio == "1") {
        radioTxt = "图片不清晰";
      } else if (this.viewImgRadio == "5" && this.viewImgErrorRadio == "2") {
        radioTxt = "图片不符合要求";
      } else {
        radioTxt = "";
      }

      const res = await fetchAuditUpload({
        taskRecordId: this.viewImgData.taskRecordId,
        auditDescription: radioTxt,
        auditStatus: this.viewImgRadio
      });
      this.getTaskRecord();
      this.viewImgDialog = false;
      this.$message({
        type: "success",
        message: "提交成功"
      });
    },
    async getAuditTask() {
      if (this.uploadImgData.imgList) {
        let imglist = [];
        this.uploadImgData.imgList.forEach(item => {
          imglist.push(item.response.data[0]);
        });
        await fetchAuditTask({
          taskRecordId: this.uploadImgData.taskRecordId,
          imgUrl: imglist.toString(),
          orderNum: this.uploadImgData.orderNum
        });
        this.showUploadImgDialog = false;
        this.getTaskRecord();
        this.$message({
          type: "success",
          message: "提交成功"
        });
      } else {
        this.$message({
          type: "error",
          message: "请选择图片"
        });
      }
    },
    async getTaskLis() {
      const res = await fetchTaskList();
      res.data.forEach(item => {
        const task = {};
        task.label = item.taskName;
        task.value = item.id;
        this.taskStatus.push(task);
      });
    },
    async getTaskRecord() {
      this.dataTable.hasOperation = false;
      this.dataTable.operation = {};
      this.dataTable.tr = [];
      if (this.taskCompleteValue == "2") {
        if (this.taskStatusValue == "" || this.taskStatusValue == "10") {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            }
          ];

          this.dataTable.hasOperation = true;
          this.dataTable.operation = {
            label: "操作",
            data: [
              {
                label: "上传&审核", // 按钮文字
                Fun: "check", // 点击按钮后触发的父组件事件
                size: "mini", // 按钮大小
                id: "1" // 按钮循环组件的key值
              }
            ]
          };
        } else {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            }
          ];
          this.dataTable.hasOperation = false;
        }
      } else if (this.taskCompleteValue == "5") {
        if (
          this.taskStatusValue == "" ||
          this.taskStatusValue == "5" ||
          this.taskStatusValue == "6" ||
          this.taskStatusValue == "7" ||
          this.taskStatusValue == "8" ||
          this.taskStatusValue == "9"
        ) {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            },
            {
              id: "5",
              label: "上传时间",
              prop: "createTime",
              show: true
            },
            {
              id: "6",
              label: "上传内容",
              prop: "imgUrl",
              show: "template"
            },
            {
              id: "7",
              label: "状态",
              prop: "actionStatus",
              show: true
            }
          ];

          this.dataTable.hasOperation = true;
          this.dataTable.operation = {
            label: "操作",
            data: [
              {
                label: "审核", // 按钮文字
                Fun: "view", // 点击按钮后触发的父组件事件
                size: "mini", // 按钮大小
                id: "2" // 按钮循环组件的key值
              }
            ]
          };
        } else {
          this.dataTable.tr = [];
          this.dataTable.hasOperation = false;
          this.dataTable.operation = {};
        }
      } else if (
        this.taskCompleteValue == "1" ||
        this.taskCompleteValue == "3"
      ) {
        if (
          this.taskStatusValue == "5" ||
          this.taskStatusValue == "6" ||
          this.taskStatusValue == "7" ||
          this.taskStatusValue == "8" ||
          this.taskStatusValue == "9"
        ) {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            },
            {
              id: "5",
              label: "上传时间",
              prop: "createTime",
              show: true
            },
            {
              id: "7",
              label: "过审人员",
              prop: "reviewers",
              show: true
            },
            {
              id: "6",
              label: "上传内容",
              prop: "imgUrl",
              show: true
            },
            {
              id: "8",
              label: "状态",
              prop: "actionStatus",
              show: true
            }
          ];
          this.dataTable.hasOperation = false;
        } else if (
          this.taskStatusValue == "2" ||
          this.taskStatusValue == "3" ||
          this.taskStatusValue == "4"
        ) {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            },
            {
              label: "完成时间",
              prop: "reviewers",
              show: true
            },
            {
              label: "状态",
              prop: "actionStatus",
              show: true
            }
          ];
          this.dataTable.hasOperation = false;
        } else if (this.taskStatusValue == "10") {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            },
            {
              id: "7",
              label: "过审人员",
              prop: "reviewers",
              show: true
            },
            {
              id: "6",
              label: "上传内容",
              prop: "imgUrl",
              show: "template"
            },
            {
              id: "9",
              label: "订单号",
              prop: "orderNum",
              show: true
            },
            {
              id: "8",
              label: "状态",
              prop: "actionStatus",
              show: true
            }
          ];
          this.dataTable.hasOperation = false;
        } else {
          this.dataTable.tr = [
            {
              id: "1",
              label: "ID",
              prop: "userId",
              show: true
            },
            {
              id: "2",
              label: "手机号",
              prop: "userName",
              show: true
            },
            {
              id: "3",
              label: "昵称",
              prop: "nickName",
              show: true
            },
            {
              id: "4",
              label: "任务名称",
              prop: "taskName",
              show: true
            },
            {
              id: "5",
              label: "上传时间",
              prop: "createTime",
              show: true
            },
            {
              id: "10",
              label: "过审时间",
              prop: "auditTime",
              show: true
            },
            {
              id: "7",
              label: "过审人员",
              prop: "reviewers",
              show: true
            },
            {
              id: "6",
              label: "上传内容",
              prop: "imgUrl",
              show: "template"
            },
            {
              id: "9",
              label: "订单号",
              prop: "orderNum",
              show: true
            },
            {
              id: "8",
              label: "状态",
              prop: "actionStatus",
              show: true
            }
          ];
          this.dataTable.hasOperation = false;
        }
      }

      this.dataTable.loading = true;
      this.searchLoading = true;
      const res = await fetchTaskRecord({
        pageSize: this.limit,
        pageNo: this.currentPage,
        key: this.searchInpt,
        searchField: this.searchSelectValue,
        taskId: this.taskStatusValue,
        actionStatus: this.taskCompleteValue
      });
      this.searchLoading = false;
      this.dataTable.loading = false;
      this.dataTable.data = [];
      this.count = res.data.total;
      res.data.list.forEach(item => {
        const task = {};
        task.userId = item.userId;
        task.taskRecordId = item.taskRecordId;
        task.nickName = item.nickName;
        task.userName = item.userName;
        task.orderNum = item.orderNum;
        task.reviewers = item.reviewers;
        task.taskName = item.taskName;
        task.createTime = this.formatterTime(item.createTime);
        task.auditTime = this.formatterTime(item.auditTime);
        task.imgUrl = item.imgUrl;
        task.reward = item.reward;
        if (item.actionStatus == "1") {
          task.actionStatus = "可领取奖励";
        } else if (item.actionStatus == "2") {
          task.actionStatus = "进行中";
        } else if (item.actionStatus == "3") {
          task.actionStatus = "已领取奖励";
        } else if (item.actionStatus == "4") {
          task.actionStatus = "下架";
        } else if (item.actionStatus == "5") {
          task.actionStatus = "任务审核中";
        } else if (item.actionStatus == "6") {
          task.actionStatus = "完成";
        }
        this.dataTable.data.push(task);
      });
    }
  }
};
</script>
<style lang="less" scoped>
.demo-image__lazy {
  height: 400px;
  overflow-y: auto;
  img {
    width: 100%;
  }
}
</style>
